home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
CC_C
/
0151.ZIP
/
WSPM.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-02-09
|
3KB
|
140 lines
#include </lib/ascii.h>
/* Convert WordStar files to Mate. Removes hard returns. */
char ibuff[2000];
char obuff[2000];
int ifile,ofile,in,on;
main(argc,argv)
int argc;
char *argv[];
{
int i,state;
char name[80],c,lastc;
printf("WordStar to Pmate Converter\r\n");
if (argc < 2) {
printf("What file?\r\n");
exit(1);
}
cpyarg(name,argv[1]);
ifile= _xopen(name,0);
if (ifile == -1) {
printf("Cannot find %s\r\n",name);
exit(2);
}
ofile= _xcreate("wspm.$$$",1);
if (ofile == -1) {
printf("Cannot create work file wspm.$$$\r\n");
exit(2);
}
state= 0;
on= 0;
while (in= _xread(ifile,ibuff,sizeof(ibuff))) {
for (i= 0; i < in; i++) {
c= ibuff[i];
if (c >= 127) switch(c) {
case CR + 128: c= CR; break;
case LF + 128:
case DEL:
case 224: c= '\0'; break;
default: c &= 0x7f; break;
} else if (c < ' ') switch (c) {
case CR: c= CR; break;
case TAB: c= TAB; break;
default: c= '\0'; break;
}
/* State machine for stripping CRs and left margins. We exchange soft
CRs for hard ones unless we get two CRs in a row. We toss spaces after
soft CRs. */
switch (state) {
/* Last char was an ordinary character */
case 0: if (c == '\0') break;
if (c == CR) state= 1;
else putc(c);
break;
/* Last character was a CR */
case 1: if (c == '\0') break;
if (c == CR) {
putc(CR); /* two in a row */
putc(LF); /* write them out */
putc(CR); /* two in a row */
putc(LF); /* write them out */
state= 0;
break;
} else {
putc(CR + 128); /* replace w/ soft */
putc(LF); /* return, */
state= 2; /* fall through ... */
}
/* last character was a soft CR */
case 2: if (c == '\0') break;
if (c == CR) {
putc(CR);
putc(LF);
state= 1;
} else if ((c == ' ') || (c == TAB)) break;
else {
putc(c);
state= 0;
}
break;
}
}
}
_xclose(ifile);
putc(SUB);
oflush();
_xclose(ofile);
_xdelete("wspm.bak");
_xrename(name,"wspm.bak");
_xrename("wspm.$$$",name);
exit(0);
}
/* Put a character into the buffer; if its full, flush to disk. */
putc(c)
char c;
{
if (on >= sizeof(obuff)) oflush();
obuff[on++]= c;
}
/* Flush the output buffer. */
oflush() {
if (_xwrite(ofile,obuff,on) != on) {
printf("Disk full!\r\n");
_xclose(ofile);
_xclose(ifile);
exit(1);
}
on= 0;
}